var e = require("../../../../@babel/runtime/helpers/objectSpread2"), r = require("../../../../@babel/runtime/helpers/typeof"), t = /%[sdj%]/g;

function n(e) {
    if (!e || !e.length) return null;
    var r = {};
    return e.forEach(function(e) {
        var t = e.field;
        r[t] = r[t] || [], r[t].push(e);
    }), r;
}

function i() {
    for (var e = arguments.length, r = new Array(e), n = 0; n < e; n++) r[n] = arguments[n];
    var i = 1, a = r[0], s = r.length;
    if ("function" == typeof a) return a.apply(null, r.slice(1));
    if ("string" == typeof a) {
        for (var u = String(a).replace(t, function(e) {
            if ("%%" === e) return "%";
            if (i >= s) return e;
            switch (e) {
              case "%s":
                return String(r[i++]);

              case "%d":
                return Number(r[i++]);

              case "%j":
                try {
                    return JSON.stringify(r[i++]);
                } catch (e) {
                    e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
                    return "[Circular]";
                }
                break;

              default:
                return e;
            }
        }), o = r[i]; i < s; o = r[++i]) u += " ".concat(o);
        return u;
    }
    return a;
}

function a(e, r) {
    return null == e || !("array" !== r || !Array.isArray(e) || e.length) || !(!function(e) {
        return "string" === e || "url" === e || "hex" === e || "email" === e || "pattern" === e;
    }(r) || "string" != typeof e || e);
}

function s(e, r, t) {
    var n = 0, i = e.length;
    !function a(s) {
        if (s && s.length) t(s); else {
            var u = n;
            n += 1, u < i ? r(e[u], a) : t([]);
        }
    }([]);
}

function u(e, r, t, i) {
    if (r.first) {
        var a = new Promise(function(r, a) {
            s(function(e) {
                var r = [];
                return Object.keys(e).forEach(function(t) {
                    r.push.apply(r, e[t]);
                }), r;
            }(e), t, function(e) {
                return i(e), e.length ? a({
                    errors: e,
                    fields: n(e)
                }) : r();
            });
        });
        return a.catch(function(e) {
            return e;
        }), a;
    }
    var u = r.firstFields || [];
    !0 === u && (u = Object.keys(e));
    var o = Object.keys(e), f = o.length, l = 0, d = [], c = new Promise(function(r, a) {
        var c = function(e) {
            if (d.push.apply(d, e), ++l === f) return i(d), d.length ? a({
                errors: d,
                fields: n(d)
            }) : r();
        };
        o.length || (i(d), r()), o.forEach(function(r) {
            var n = e[r];
            -1 !== u.indexOf(r) ? s(n, t, c) : function(e, r, t) {
                var n = [], i = 0, a = e.length;
                function s(e) {
                    n.push.apply(n, e), ++i === a && t(n);
                }
                e.forEach(function(e) {
                    r(e, s);
                });
            }(n, t, c);
        });
    });
    return c.catch(function(e) {
        return e;
    }), c;
}

function o(e) {
    return function(r) {
        return r && r.message ? (r.field = r.field || e.fullField, r) : {
            message: "function" == typeof r ? r() : r,
            field: r.field || e.fullField
        };
    };
}

function f(t, n) {
    if (n) for (var i in n) if (n.hasOwnProperty(i)) {
        var a = n[i];
        "object" == r(a) && "object" == r(t[i]) ? t[i] = e(e({}, t[i]), a) : t[i] = a;
    }
    return t;
}

function l(e, r, t, n, s, u) {
    !e.required || t.hasOwnProperty(e.field) && !a(r, u || e.type) || n.push(i(s.messages.required, e.fullField));
}

"undefined" != typeof process && process.env;

var d = {
    email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
    url: new RegExp("^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$", "i"),
    hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i
}, c = {
    integer: function(e) {
        return /^(-)?\d+$/.test(e);
    },
    float: function(e) {
        return /^(-)?\d+(\.\d+)?$/.test(e);
    },
    array: function(e) {
        return Array.isArray(e);
    },
    regexp: function(e) {
        if (e instanceof RegExp) return !0;
        try {
            return !!new RegExp(e);
        } catch (e) {
            e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
            return !1;
        }
    },
    date: function(e) {
        return "function" == typeof e.getTime && "function" == typeof e.getMonth && "function" == typeof e.getYear;
    },
    number: function(e) {
        return !isNaN(e) && "number" == typeof +e;
    },
    object: function(e) {
        return "object" == r(e) && !c.array(e);
    },
    method: function(e) {
        return "function" == typeof e;
    },
    email: function(e) {
        return "string" == typeof e && !!e.match(d.email) && e.length < 255;
    },
    url: function(e) {
        return "string" == typeof e && !!e.match(d.url);
    },
    hex: function(e) {
        return "string" == typeof e && !!e.match(d.hex);
    }
}, p = {
    required: l,
    whitespace: function(e, r, t, n, a) {
        (/^\s+$/.test(r) || "" === r) && n.push(i(a.messages.whitespace, e.fullField));
    },
    type: function(e, t, n, a, s) {
        if (e.required && void 0 === t) l(e, t, n, a, s); else {
            var u = e.type;
            [ "integer", "float", "array", "regexp", "object", "method", "email", "number", "date", "url", "hex" ].indexOf(u) > -1 ? c[u](t) || a.push(i(s.messages.types[u], e.fullField, e.type)) : u && r(t) !== e.type && a.push(i(s.messages.types[u], e.fullField, e.type));
        }
    },
    range: function(e, r, t, n, a) {
        var s = "number" == typeof e.len, u = "number" == typeof e.min, o = "number" == typeof e.max, f = r, l = null, d = "number" == typeof r, c = "string" == typeof r, p = Array.isArray(r);
        if (d ? l = "number" : c ? l = "string" : p && (l = "array"), !l) return !1;
        p && (f = r.length), c && (f = r.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "_").length), 
        s ? f !== e.len && n.push(i(a.messages[l].len, e.fullField, e.len)) : u && !o && f < e.min ? n.push(i(a.messages[l].min, e.fullField, e.min)) : o && !u && f > e.max ? n.push(i(a.messages[l].max, e.fullField, e.max)) : u && o && (f < e.min || f > e.max) && n.push(i(a.messages[l].range, e.fullField, e.min, e.max));
    },
    enum: function(e, r, t, n, a) {
        e.enum = Array.isArray(e.enum) ? e.enum : [], -1 === e.enum.indexOf(r) && n.push(i(a.messages.enum, e.fullField, e.enum.join(", ")));
    },
    pattern: function(e, r, t, n, a) {
        e.pattern && (e.pattern instanceof RegExp ? (e.pattern.lastIndex = 0, e.pattern.test(r) || n.push(i(a.messages.pattern.mismatch, e.fullField, r, e.pattern))) : "string" == typeof e.pattern && (new RegExp(e.pattern).test(r) || n.push(i(a.messages.pattern.mismatch, e.fullField, r, e.pattern))));
    }
};

function y(e, r, t, n, i) {
    var s = e.type, u = [];
    if (e.required || !e.required && n.hasOwnProperty(e.field)) {
        if (a(r, s) && !e.required) return t();
        p.required(e, r, n, u, i, s), a(r, s) || p.type(e, r, n, u, i);
    }
    t(u);
}

var h = {
    string: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r, "string") && !e.required) return t();
            p.required(e, r, n, s, i, "string"), a(r, "string") || (p.type(e, r, n, s, i), p.range(e, r, n, s, i), 
            p.pattern(e, r, n, s, i), !0 === e.whitespace && p.whitespace(e, r, n, s, i));
        }
        t(s);
    },
    method: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i), void 0 !== r && p.type(e, r, n, s, i);
        }
        t(s);
    },
    number: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if ("" === r && (r = void 0), a(r) && !e.required) return t();
            p.required(e, r, n, s, i), void 0 !== r && (p.type(e, r, n, s, i), p.range(e, r, n, s, i));
        }
        t(s);
    },
    boolean: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i), void 0 !== r && p.type(e, r, n, s, i);
        }
        t(s);
    },
    regexp: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i), a(r) || p.type(e, r, n, s, i);
        }
        t(s);
    },
    integer: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i), void 0 !== r && (p.type(e, r, n, s, i), p.range(e, r, n, s, i));
        }
        t(s);
    },
    float: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i), void 0 !== r && (p.type(e, r, n, s, i), p.range(e, r, n, s, i));
        }
        t(s);
    },
    array: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r, "array") && !e.required) return t();
            p.required(e, r, n, s, i, "array"), a(r, "array") || (p.type(e, r, n, s, i), p.range(e, r, n, s, i));
        }
        t(s);
    },
    object: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i), void 0 !== r && p.type(e, r, n, s, i);
        }
        t(s);
    },
    enum: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i), void 0 !== r && p.enum(e, r, n, s, i);
        }
        t(s);
    },
    pattern: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r, "string") && !e.required) return t();
            p.required(e, r, n, s, i), a(r, "string") || p.pattern(e, r, n, s, i);
        }
        t(s);
    },
    date: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            var u;
            if (p.required(e, r, n, s, i), !a(r)) u = "number" == typeof r ? new Date(r) : r, 
            p.type(e, u, n, s, i), u && p.range(e, u.getTime(), n, s, i);
        }
        t(s);
    },
    url: y,
    hex: y,
    email: y,
    required: function(e, t, n, i, a) {
        var s = [], u = Array.isArray(t) ? "array" : r(t);
        p.required(e, t, i, s, a, u), n(s);
    },
    any: function(e, r, t, n, i) {
        var s = [];
        if (e.required || !e.required && n.hasOwnProperty(e.field)) {
            if (a(r) && !e.required) return t();
            p.required(e, r, n, s, i);
        }
        t(s);
    }
};

function g() {
    return {
        default: "Validation error on field %s",
        required: "%s is required",
        enum: "%s must be one of %s",
        whitespace: "%s cannot be empty",
        date: {
            format: "%s date %s is invalid for format %s",
            parse: "%s date could not be parsed, %s is invalid ",
            invalid: "%s date %s is invalid"
        },
        types: {
            string: "%s is not a %s",
            method: "%s is not a %s (function)",
            array: "%s is not an %s",
            object: "%s is not an %s",
            number: "%s is not a %s",
            date: "%s is not a %s",
            boolean: "%s is not a %s",
            integer: "%s is not an %s",
            float: "%s is not a %s",
            regexp: "%s is not a valid %s",
            email: "%s is not a valid %s",
            url: "%s is not a valid %s",
            hex: "%s is not a valid %s"
        },
        string: {
            len: "%s must be exactly %s characters",
            min: "%s must be at least %s characters",
            max: "%s cannot be longer than %s characters",
            range: "%s must be between %s and %s characters"
        },
        number: {
            len: "%s must equal %s",
            min: "%s cannot be less than %s",
            max: "%s cannot be greater than %s",
            range: "%s must be between %s and %s"
        },
        array: {
            len: "%s must be exactly %s in length",
            min: "%s cannot be less than %s in length",
            max: "%s cannot be greater than %s in length",
            range: "%s must be between %s and %s in length"
        },
        pattern: {
            mismatch: "%s value %s does not match pattern %s"
        },
        clone: function() {
            var e = JSON.parse(JSON.stringify(this));
            return e.clone = this.clone, e;
        }
    };
}

var m = g();

function v(e) {
    this.rules = null, this._messages = m, this.define(e);
}

v.prototype = {
    messages: function(e) {
        return e && (this._messages = f(g(), e)), this._messages;
    },
    define: function(e) {
        if (!e) throw new Error("Cannot configure a schema with no rules");
        if ("object" != r(e) || Array.isArray(e)) throw new Error("Rules must be an object");
        var t, n;
        for (t in this.rules = {}, e) e.hasOwnProperty(t) && (n = e[t], this.rules[t] = Array.isArray(n) ? n : [ n ]);
    },
    validate: function(t, a, s) {
        var l = this;
        void 0 === a && (a = {}), void 0 === s && (s = function() {});
        var d, c, p = t, y = a, h = s;
        if ("function" == typeof y && (h = y, y = {}), !this.rules || 0 === Object.keys(this.rules).length) return h && h(), 
        Promise.resolve();
        if (y.messages) {
            var q = this.messages();
            q === m && (q = g()), f(q, y.messages), y.messages = q;
        } else y.messages = this.messages();
        var b = {};
        (y.keys || Object.keys(this.rules)).forEach(function(r) {
            d = l.rules[r], c = p[r], d.forEach(function(n) {
                var i = n;
                "function" == typeof i.transform && (p === t && (p = e({}, p)), c = p[r] = i.transform(c)), 
                (i = "function" == typeof i ? {
                    validator: i
                } : e({}, i)).validator = l.getValidationMethod(i), i.field = r, i.fullField = i.fullField || r, 
                i.type = l.getType(i), i.validator && (b[r] = b[r] || [], b[r].push({
                    rule: i,
                    value: c,
                    source: p,
                    field: r
                }));
            });
        });
        var w = {};
        return u(b, y, function(t, n) {
            var a, s = t.rule, u = !("object" !== s.type && "array" !== s.type || "object" != r(s.fields) && "object" != r(s.defaultField));
            function f(r, t) {
                return e(e({}, t), {}, {
                    fullField: "".concat(s.fullField, ".").concat(r)
                });
            }
            function l(r) {
                void 0 === r && (r = []);
                var a = r;
                if (Array.isArray(a) || (a = [ a ]), !y.suppressWarning && a.length && v.warning("async-validator:", a), 
                a.length && s.message && (a = [].concat(s.message)), a = a.map(o(s)), y.first && a.length) return w[s.field] = 1, 
                n(a);
                if (u) {
                    if (s.required && !t.value) return a = s.message ? [].concat(s.message).map(o(s)) : y.error ? [ y.error(s, i(y.messages.required, s.field)) ] : [], 
                    n(a);
                    var l = {};
                    if (s.defaultField) for (var d in t.value) t.value.hasOwnProperty(d) && (l[d] = s.defaultField);
                    for (var c in l = e(e({}, l), t.rule.fields)) if (l.hasOwnProperty(c)) {
                        var p = Array.isArray(l[c]) ? l[c] : [ l[c] ];
                        l[c] = p.map(f.bind(null, c));
                    }
                    var h = new v(l);
                    h.messages(y.messages), t.rule.options && (t.rule.options.messages = y.messages, 
                    t.rule.options.error = y.error), h.validate(t.value, t.rule.options || y, function(e) {
                        var r = [];
                        a && a.length && r.push.apply(r, a), e && e.length && r.push.apply(r, e), n(r.length ? r : null);
                    });
                } else n(a);
            }
            u = u && (s.required || !s.required && t.value), s.field = t.field, s.asyncValidator ? a = s.asyncValidator(s, t.value, l, t.source, y) : s.validator && (!0 === (a = s.validator(s, t.value, l, t.source, y)) ? l() : !1 === a ? l(s.message || "".concat(s.field, " fails")) : a instanceof Array ? l(a) : a instanceof Error && l(a.message)), 
            a && a.then && a.then(function() {
                return l();
            }, function(e) {
                return l(e);
            });
        }, function(e) {
            !function(e) {
                var r, t = [], i = {};
                function a(e) {
                    var r;
                    Array.isArray(e) ? t = (r = t).concat.apply(r, e) : t.push(e);
                }
                for (r = 0; r < e.length; r++) a(e[r]);
                t.length ? i = n(t) : (t = null, i = null), h(t, i);
            }(e);
        });
    },
    getType: function(e) {
        if (void 0 === e.type && e.pattern instanceof RegExp && (e.type = "pattern"), "function" != typeof e.validator && e.type && !h.hasOwnProperty(e.type)) throw new Error(i("Unknown rule type %s", e.type));
        return e.type || "string";
    },
    getValidationMethod: function(e) {
        if ("function" == typeof e.validator) return e.validator;
        var r = Object.keys(e), t = r.indexOf("message");
        return -1 !== t && r.splice(t, 1), 1 === r.length && "required" === r[0] ? h.required : h[this.getType(e)] || !1;
    }
}, v.register = function(e, r) {
    if ("function" != typeof r) throw new Error("Cannot register a validator by type, validator is not a function");
    h[e] = r;
}, v.warning = function() {}, v.messages = m, exports.Schema = v;